package cn.eng.basic.utils;

import cn.eng.basic.jwt.JwtUtils;
import cn.eng.basic.jwt.LoginData;
import cn.eng.basic.jwt.Payload;
import cn.eng.basic.jwt.RsaUtils;
import cn.eng.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * 工具类: 获取当前人登录人的信息
 */
public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request) throws Exception {
        //1.获取请求头里面的token
        String token = request.getHeader("token");
        if (token != null){
               //2.获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
               //3.使用工具类解密获取载荷PayLoad
              Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
              //没有报错 就登陆了
              return payload.getLoginData().getLogininfo();
        }
        return null;
    }
}
